package net.brokenroad.gamma;

import java.awt.*;
import java.util.*;
import javax.swing.*;

import java.awt.event.*;
import net.brokenroad.gamma.gui.*;
import net.brokenroad.gamma.net.*;

public class Gamma implements ActionListener, SessionListener
{
	ArrayList<Session> sessions;
	JFrame frame;
	JMenuBar menuBar;
	JMenu menu;
	JMenuItem startServer, startSession, endSession;

	private void addComponentsToPane(Container pane)
	{
		menuBar = new JMenuBar();
		menu = new JMenu("Gamma");
		menuBar.add(menu);		
		
		startSession = new JMenuItem("Start Session...");
		startSession.addActionListener(this);
		menu.add(startSession);

		endSession = new JMenuItem("End Session");
		endSession.addActionListener(this);
		menu.add(endSession);
		
		menu.addSeparator();
		startServer = new JMenuItem("Start Server...");
		startServer.addActionListener(this);
		menu.add(startServer);

		frame.setJMenuBar(menuBar);
		
		new GammaGUIMockup();
	}

	private void createAndShowGUI()
	{
		JFrame.setDefaultLookAndFeelDecorated(true);

		frame = new JFrame("Gamma v0.01");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		addComponentsToPane(frame.getContentPane());
		frame.pack();
		frame.setSize(640, 480);
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == startServer)
		{
			SessionStartFrame ssf = new SessionStartFrame();
			ssf.startSession(this);
		}
		else if(ae.getSource() == startSession)
		{
			;// write me!
		}
		else if(ae.getSource() == endSession)
		{

		} else
			System.out.println("Action performed. Source was: " + ae.getSource());
	}

	public void sessionChange(Session newSess, String whatHappened)
	{
		if(whatHappened.equals("add"))
			if(!sessions.contains(newSess))
				sessions.add(newSess);
			else
				JOptionPane.showMessageDialog(frame, "Can't send packets without establishing a connection!", "Error", JOptionPane.ERROR_MESSAGE);
	}

	public Gamma()
	{
		sessions = new ArrayList<Session>(1);
		createAndShowGUI();
	}

	public static void main(String[] args)
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e)
		{
		}

		new Gamma();
	}
}
